package com.jacobson.core.framework.errorhandler

import android.content.Context
import com.jacobson.core.widget.logger.LoggerFactory

/**
 * ================================================
 *
 *  @author Wucz on 2018/7/24 11:49
 * ================================================
 */
abstract class OnResponseErrorListener : OnErrorListener {
    object EMPTY : OnResponseErrorListener() {
        private val logger = LoggerFactory.getLogger("GlobalError")
        override fun handleResponseError(context: Context, code: Int, message: String?): Boolean {
            logger.e("ResponseError($code): $message")
            return true
        }

        override fun handleOtherError(context: Context, e: Throwable) {
            logger.e("UnknownError: ", e)
        }
    }

    override fun onError(context: Context, e: Throwable) {
        (e as? ResponseException)?.let {
            handleResponseError(context, it.code, it.message)
        } ?: handleOtherError(context, e)
    }

    open fun handleOtherError(context: Context, e: Throwable) = Unit

    abstract fun handleResponseError(context: Context, code: Int, message: String?): Boolean

}